
const TOKEN_KEY = 'token';
const USER_KEY = 'userId';
const TABLENAME = "tableName"
const IP_ADDRESS = "ipAddress"
const isLogin = () => {
  return !!localStorage.getItem(TOKEN_KEY);
};
const getToken = () => {
  return localStorage.getItem(TOKEN_KEY);
};

const setToken = (token: string) => {
  localStorage.setItem(TOKEN_KEY, token);
};

const getUserId = () => {
  return localStorage.getItem(USER_KEY);
};

const setTableName = (token: string) => {
  localStorage.setItem(TABLENAME, token);
};

const getTableName = () => {
  return localStorage.getItem(TABLENAME);
};

const setUserId = (token: string) => {
  localStorage.setItem(USER_KEY, token);
};

const clearToken = () => {
  localStorage.removeItem(TOKEN_KEY);
  localStorage.removeItem(USER_KEY);
  localStorage.removeItem(TABLENAME);
  localStorage.removeItem("urAvatar");
  localStorage.removeItem("router");
  localStorage.removeItem("userName");
  localStorage.removeItem(IP_ADDRESS);
  localStorage.removeItem("user");
};

const checkIp = () => {
  if (!localStorage.getItem(IP_ADDRESS)) {
    const url = 'https://ip-api.io/json';
    fetch(url)
      .then(response => response.json())
      .then(data => {
        localStorage.setItem(IP_ADDRESS, data.ip)
      });
  }
}
const getIpAddress = () => {
  return localStorage.getItem(IP_ADDRESS)
}

export { isLogin, getToken, setToken, clearToken, getUserId, setUserId, setTableName, getTableName,getIpAddress,checkIp };
